package com.tepth.remote.control.delegate.picture.list;

import com.tepth.remote.control.base.bean.PictureBean;
import com.tepth.remote.ui.recycler.BaseDataConverter;
import com.tepth.remote.ui.recycler.ItemType;
import com.tepth.remote.ui.recycler.MultipleFields;
import com.tepth.remote.ui.recycler.MultipleItemEntity;

import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/22
 */
public class PictureListDataConvert extends BaseDataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        final ArrayList<SoapObject> soapArray = getSoapArray();
        final int size = soapArray.size();
        for (int i = 0; i < size; i++) {
            final SoapObject soapObject = soapArray.get(i);
            PictureBean bean = new PictureBean()
                    .setId(soapObject.getPropertySafelyAsString("FI_Id"))
                    .setName(soapObject.getPropertySafelyAsString("U_Name"))
                    .setFenceName(soapObject.getPropertySafelyAsString("FI_Name"))
                    .setLatitude(Double.parseDouble(soapObject.getPropertySafelyAsString("FI_X")))
                    .setLongitude(Double.parseDouble(soapObject.getPropertySafelyAsString("FI_Y")))
                    .setPhotoDate(soapObject.getPropertySafelyAsString("FI_PhotoDate"))
                    .setFullUrl(soapObject.getPropertySafelyAsString("FI_FullUrl"))
                    .setSize(soapObject.getPropertySafelyAsString("FI_Size"))
                    .setProjectId(soapObject.getPropertySafelyAsString("FK_Pro_Id"))
                    .setProjectName(soapObject.getPropertySafelyAsString("FK_Pro_Name"))
                    .setFenceId(soapObject.getPropertySafelyAsString("FK_Elec_Id"))
                    .setUserId(soapObject.getPropertySafelyAsString("FI_UserId"))
                    .setCreateDate(soapObject.getPropertySafelyAsString("FI_CreateDate"));
            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setItemType(ItemType.PICTURE_LIST)
                    .setField(MultipleFields.TITLE, bean.getName())
                    .setField(MultipleFields.TEXT, bean.getCreateDate())
                    .setField(MultipleFields.NAME, bean.getProjectName())
                    .setField(MultipleFields.ID, bean.getId())
                    .setField(MultipleFields.PICTURE_BEAN, bean)
                    .builder();
            ENTITIES.add(entity);
        }
        return ENTITIES;
    }

    private ArrayList<SoapObject> getSoapArray() {
        final ArrayList<SoapObject> soapArray = new ArrayList<>();
        final ArrayList<Object> array = getArrays();
        final int size = array.size();
        for (int i = 0; i < size; i++) {
            soapArray.add((SoapObject) array.get(i));
        }
        return soapArray;
    }
}
